package bbs;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import util.DB;

public class BoardAccessRuleDAO {

    private static BoardAccessRule makeBoardAccessRule(ResultSet rs) throws SQLException {
        BoardAccessRule obj = new BoardAccessRule();
        obj.setId(rs.getInt("Id"));
        obj.setBoardId(rs.getInt("boardId"));
        obj.setUserGroupId(rs.getInt("userGroupId"));
        obj.setListArticles(rs.getBoolean("listArticles"));
        obj.setCreateArticle(rs.getBoolean("createArticle"));
        obj.setReadArticle(rs.getBoolean("readArticle"));
        obj.setReadMyArticle(rs.getBoolean("readMyArticle"));
        obj.setEditArticle(rs.getBoolean("editArticle"));
        obj.setEditMyArticle(rs.getBoolean("editMyArticle"));
        obj.setDeleteArticle(rs.getBoolean("deleteArticle"));
        obj.setDeleteMyArticle(rs.getBoolean("deleteMyArticle"));
        obj.setCreateComment(rs.getBoolean("createComment"));
        obj.setManageBoard(rs.getBoolean("manageBoard"));
        return obj;
    }

    public static BoardAccessRule selectByUserId(int boardId, int userId) throws Exception {
        String sql = "EXEC bbs.boardAccessRuleSelectByUserId ?, ?";
        try (Connection con = DB.getConnection();
             PreparedStatement stmt = con.prepareStatement(sql)) {
            stmt.setInt(1, boardId);
            stmt.setInt(2, userId);
            ResultSet rs = stmt.executeQuery();
            return rs.next() ? makeBoardAccessRule(rs) : null;
        }
    }
}
